unit oFormArrivalData;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, oFrameArrivalData, ExtCtrls, StdCtrls, oGeneratorData, oManager;

const
  typeNew = 1;
  typeEdit = 2;

type
  TFormArrivalData = class(TForm)
    aFrame: TfArrival;
    procedure aFrametxtArriveCountClick(Sender: TObject);
    procedure aFrametxtArriveEveryClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure aFramebtnOKClick(Sender: TObject);
    procedure aFramebtnCancelClick(Sender: TObject);
    procedure aFrametxtArrivesFromExit(Sender: TObject);
    procedure aFrametxtArrivesToExit(Sender: TObject);
    procedure aFrametxtNameExit(Sender: TObject);
    procedure aFramecbxPedestriansExit(Sender: TObject);
    procedure aFramecbxPathsExit(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure aFramebtnEditClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    aArriveEvery: TGeneratorData;
    aArriveCount: TGeneratorData;
    aMaster: TManager;
    aType: Integer;
    aOldEvery: TGeneratorData;
    aOldCount: TGeneratorData;
    procedure checkValue(paTextBox: TEdit);
    procedure tryEnableCommitBtn;
  public
    property ArriveEvery: TGeneratorData read aArriveEvery write aArriveEvery;
    property ArriveCount: TGeneratorData read aArriveCount write aArriveCount;
    property Master: TManager read aMaster write aMaster;
    property ShowType: Integer read aType write aType;
    { Public declarations }
  end;

var
  FormArrivalData: TFormArrivalData;

implementation

uses
  oProcessItem, oProcessObjects, oProcessObjectConstants;

resourcestring notANumber = 'is not a number';
resourcestring arrivalAlreadyExists = 'already exists';
resourcestring arrivalNotExists = 'entity not exists';

{$R *.dfm}

procedure TFormArrivalData.aFramebtnCancelClick(Sender: TObject);
begin
  aArriveEvery := aOldEvery;
  aArriveCount := aOldCount;
  CloseModal;
end;

procedure TFormArrivalData.aFramebtnEditClick(Sender: TObject);
begin
  CloseModal;
end;

procedure TFormArrivalData.aFramebtnOKClick(Sender: TObject);
begin
  //kontrola na data, ci su ok
  CloseModal;
end;

procedure TFormArrivalData.aFramecbxPathsExit(Sender: TObject);
var
  item: TProcessItem;
begin
  item := Master.ProcessObject(TProcessItem.create(PO_PATHS_GET_PATH,TString.Create(aFrame.cbxPaths.Text)));
  if (aFrame.cbxPaths.ItemIndex < 0) and (item.Code = PO_FAILURE) then
    begin
      MessageDlg(arrivalNotExists,mtError,[mbOK],0);
      aFrame.cbxPaths.SetFocus;
    end;
  item.Free;
end;

procedure TFormArrivalData.aFramecbxPedestriansExit(Sender: TObject);
var
  item: TProcessItem;
begin
  item := Master.ProcessObject(TProcessItem.create(PO_PEDMAN_EXISTS,TString.Create(aFrame.cbxPedestrians.Text)));
  if (aFrame.cbxPedestrians.ItemIndex < 0) and (item.Code = PO_FAILURE) then
    begin
      MessageDlg(arrivalNotExists,mtError,[mbOK],0);
      aFrame.cbxPedestrians.SetFocus;
    end;
  item.Free;
end;

procedure TFormArrivalData.aFrametxtArriveCountClick(Sender: TObject);
var
  item: TProcessItem;
begin
  //povedat manazerovi, ze sa kliklo a chce generator
  item := Master.ProcessObject(TProcessItem.create(PO_GET_GENERATOR,TProcessingGeneratorData.create(aArriveCount)));
  aArriveCount := (item.Data as TProcessingGeneratorData).Data;
  if aArriveCount <> nil then aFrame.txtArriveCount.Text := aArriveCount.toString;
  tryEnableCommitBtn;
end;

procedure TFormArrivalData.aFrametxtArriveEveryClick(Sender: TObject);
var
  item: TProcessItem;
begin
  //povedat manazerovi, ze sa kliklo a chce generator
  item := Master.ProcessObject(TProcessItem.create(PO_GET_GENERATOR,TProcessingGeneratorData.create(aArriveEvery)));
  aArriveEvery := (item.Data as TProcessingGeneratorData).Data;
  if aArriveEvery <> nil then aFrame.txtArriveEvery.Text := aArriveEvery.toString;
  tryEnableCommitBtn;
end;

procedure TFormArrivalData.aFrametxtArrivesFromExit(Sender: TObject);
begin
  checkValue(aFrame.txtArrivesFrom);
end;

procedure TFormArrivalData.aFrametxtArrivesToExit(Sender: TObject);
begin
  checkValue(aFrame.txtArrivesTo);
end;

procedure TFormArrivalData.aFrametxtNameExit(Sender: TObject);
var
  item: TProcessItem;
begin
  item := Master.ProcessObject(TProcessItem.create(PO_ARRMAN_EXISTS,TString.Create(aFrame.txtName.Text)));
  if item.Code = PO_SUCCESS
    then begin
      MessageDlg(aFrame.txtName.Text + ' ' + arrivalAlreadyExists,mtError,[mbOK],0);
      aFrame.txtName.SetFocus;
    end;
end;

procedure TFormArrivalData.checkValue(paTextBox: TEdit);
begin
  try
    StrToFloat(paTextBox.Text);
  except
    MessageDlg(paTextBox.Text + ' ' + notANumber,mtError,[mbOK],0);
    paTextBox.SetFocus;
  end;
end;

procedure TFormArrivalData.FormCreate(Sender: TObject);
begin
  aArriveEvery := nil;
  aArriveCount := nil;
  aOldEvery := nil;
  aOldCount := nil;
  aMaster := nil;
  aType := typeNew;
end;

procedure TFormArrivalData.FormDestroy(Sender: TObject);
begin
  aArriveEvery := nil;
  aArriveCount := nil;
  aOldEvery := nil;
  aOldCount := nil;
  aMaster := nil;
end;

procedure TFormArrivalData.FormShow(Sender: TObject);
begin
  tryEnableCommitBtn;
  aOldEvery := aArriveEvery;
  aOldCount := aArriveCount;
end;

procedure TFormArrivalData.tryEnableCommitBtn;
begin
  if (aType = typeNew) and (aFrame.txtArriveCount.Text <> '') and (aFrame.txtArriveEvery.Text <> '')
    then aFrame.btnOK.Enabled := true
    else aFrame.btnOK.Enabled := false;
end;

end.
